# Contributor: Sven Greb <development@svengreb.de>
# Contributor: Arctic Ice Studio <development@arcticicestudio.com>

pkgname=mage
pkgver=1.14.0
pkgrel=1
pkgdesc="A Make/rake-like build tool using Go"
arch=("loong64" "x86_64")
url="https://magefile.org"
license=("Apache")
makedepends=(go)
source=($pkgname-$pkgver.tar.gz::https://github.com/magefile/mage/archive/refs/tags/v$pkgver.tar.gz)
sha256sums=('346b82cd4c6af35a6bffcdaed1a6de0947d8b282ce80ee719805e8fea33bc481')

build() {
  cd "$pkgname-$pkgver"

  # mage is build with mage itself, therefore we simluate the execution of the provided `bootstrap.go` installation
  # file that is recommended by the author.
  # The file builds mage when mage itself is not installed on the target system yet and included metadata information
  # for the binary.

  # References:
  #   1. https://github.com/magefile/mage/blob/fe9f9420/.goreleaser.yml#L27
  #   2. https://github.com/magefile/mage/blob/fe9f9420/.goreleaser.yml#L10
  export CGO_ENABLED=0
  local BUILD_DATE="$(date --rfc-3339=date --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}")"
  go build \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-X \"github.com/magefile/mage/mage.timestamp=${BUILD_DATE}\" \
              -X \"github.com/magefile/mage/mage.gitTag=v$pkgver\" \
              -extldflags \"${LDFLAGS}\"" \
    -o build/"$pkgname" .
}

package() {
  cd "$pkgname-$pkgver"
  install -Dm755 build/"$pkgname" -t "$pkgdir/usr/bin/"
  install -Dm644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}
